﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LTSMZ.life.Model;
using LTSMZ.life.BLL;
using System.Data.SqlClient;
using System.Data;

namespace LTSMZ.life.UI.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        //首页视图
        public ActionResult Index()
        {
            return View();
        }
        //兑换专区
        public ActionResult duihuan()
        {
            return View();
        }
        //台创园
        public ActionResult taichuangyuan()
        {
            return View();
        }
        //全国配送
        public ActionResult delivery()
        {
            return View();
        }
        //充值
        public ActionResult Recharge()
        {
            return View();
        }
        //超值宅配
        public ActionResult LoadEasyHomeList()
       {
           return View();
       }
        //促销
        public ActionResult offsale()
        {
            return View();
        }
        //本来礼品
        public ActionResult GiftCard()
        {
            return View();
        }
        //每日鲜
        public ActionResult fresh()
        {
            
            return View();
        }
        //详情页
        public ActionResult list()
        {
            return View();
        }
        //商品里列表页
        public ActionResult produce()
        {
            return View();
        }
        //购物车
        public ActionResult cart()
        {
            return View();
        }
        //订单
        public ActionResult order()
        {
            
            OrdersBll bll = new OrdersBll();
            if (Session["name"] != null)
            {
                List<Orders> list = bll.ShowOrders(Session["name"].ToString());
                return View("order",list);
            }
            else
            {
                return Content("<script>alert('请您登录！！');location.href='/Home/Deng'</script>");
            }
           
        }      
        //登录
        public ActionResult Deng()
        {
            return View();
        }
        //注册
        public ActionResult Zhuce()
        {
            return View();
        }
        /// <summary>
        /// 超值宅配商品展示
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadEasyHomeListShow()
        {
            ProductBll bll = new ProductBll();
            return Json(bll.show(), JsonRequestBehavior.AllowGet);
        }
        //登录功能
        public ActionResult DengMember(string name,string pwd)
        {
            MemberBll bll = new MemberBll();
            if(bll.DengMember(name,pwd)>0)
            {
                Session["mid"] = bll.DengMember(name, pwd);
                System.Web.HttpContext.Current.Session["name"] = name;
                //HttpContext.Session["name"] = name;
                return Content("1");
            }
            else
            {
                return Content("2");
            }
        }
        /// <summary>
        /// 购物车的显示
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowShopping()
        {
            ShoppingCarBll bll = new ShoppingCarBll();
                      
            System.Data.DataTable table = bll.ShowShoppingCar(Convert.ToInt32(Session["mid"]));
            List<ShoppingCar> list = new List<ShoppingCar>();
            foreach (System.Data.DataRow item in table.Rows)
            {
                ShoppingCar car = new ShoppingCar();
                car.shoppingID = Convert.ToInt32(item["shoppingID"]);
                car.Pid = Convert.ToInt32(item["Pid"]);
                car.Pname = item["Pname"].ToString();
                car.ProNum = Convert.ToInt32(item["ProNum"].ToString());
                car.PImgHref = item["PImgHref"].ToString();
                car.Price = Convert.ToDecimal(item["Price"]);
                list.Add(car);
            }
            return Json(list, JsonRequestBehavior.AllowGet);          
           
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="pwd2"></param>
        /// <returns></returns>
        public ActionResult ZhuMember(string Email,string NC,string Name,string phone,string pwd)
        {
            MemberBll bll = new MemberBll();
            Member m = new Member();
            m.Mname = Email;
            m.Mpassword = pwd;
            m.NickName = NC;
            m.RealName = Name;
            m.Sex = "男";
            m.Tel = phone;
            if(bll.ZhuMember(m)>0)
            {
                return Content("1");
            }
            else
            {
                return Content("2");
            }
        }
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public ActionResult GoShopping(ShoppingCar car)
        {         
            ShoppingCarBll bll = new ShoppingCarBll();
           
            if (Session["mid"] != null)
            {
                car.Mid = Convert.ToInt32(Session["mid"]);
                if (bll.InsertShoppingCar(car) > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("2");
                }   
            }
            else
            {
                return Content("3");
            }
                   
            
        }

        //订单添加
        public ActionResult addOrderSon(string pids,string num,string sum)
        {
            OrdersBll bll=new OrdersBll();
            Orders order = new Orders();
            order.OrderTime = DateTime.Now.ToString();
            order.Mid = Convert.ToInt32(Session["mid"]);
            order.Rid = 1;
            order.State = 0;

            if (pids == null && num == null && sum==null)
            {
                return Content("<script>alert('数据不能为空！');location.href='/Home/cart'</script>");
            }
            else
            {       
                string[] pid = pids.Split(',');
                string[] numb = num.Split(',');
                string[] sumprice =sum.Split(',');
                string xml = "<ItemList>";

                for (int i = 0; i < pid.Length; i++)
                {
                    xml += "<Item ProductNum='" + numb[i] + "' Pid='" + pid[i] + "' OrderPrice='" + sumprice[i] + "'/>";
                }
                xml += "</ItemList>";

                if(bll.InsertOrderSon(order,xml)>0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("2");
                }

            }
        }
         /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int delShoppingCar(int id)
        {
            ShoppingCarBll bll = new ShoppingCarBll();
            if(bll.delShoppingCar(id)>0)
            {
                return 1;
            }
            else
            {
                return 2;
            }
        }
        //订单提交
        public ActionResult placeorder()
        {
           return View();
        }
        //用户的地址
        public ActionResult ShowAddress()
        {
            GoodsReceiptBll bll = new GoodsReceiptBll();
            if (Session["mid"] != null)
            {
                List<GoodsReceipt> list = bll.ShowGoodsReceipt(Convert.ToInt32(Session["mid"]));
                return Json(list, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }
        //显示评论
        public ActionResult ShowComm(int id)
        {
            CommentBll bll = new CommentBll();
            return Json(bll.ShowComment(id), JsonRequestBehavior.AllowGet);
        }
        //显示当前商品的评论数
        public ActionResult ShowCount(int id)
        {
            CommentBll bll = new CommentBll();
            return Json(bll.ShowCount(id), JsonRequestBehavior.AllowGet);
        }
        ///// <summary>
        ///// 根据会员账号获取会员编号
        ///// </summary>
        ///// <param name="name"></param>
        ///// <returns></returns>
        //public ActionResult GetIdByName(string name)
        //{
        //    MemberBll bll = new MemberBll();
        //    return Json(bll.GetIdByName(name), JsonRequestBehavior.AllowGet);
        //}
        /// <summary>
        /// 根据会员名和商品编号获取该会员有没有买过该商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Pid"></param>
        /// <returns></returns>
        public ActionResult GetCountByMidandPid(int Pid)
        {
            string name = Session["name"].ToString();
            MemberBll bll=new MemberBll();
            return Json(bll.GetCountByMidandPid(name, Pid), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 判断会员有没有登录
        /// </summary>
        /// <returns></returns>
        public ActionResult GetByName()
        {
            OrdersBll bll = new OrdersBll();
            if (Session["name"] != null)
            {
                return Content("0");   
            }
            else
            {
                return Content("1");
            }
        }
        public ActionResult UserShow()
        {
            OrdersBll bll=new OrdersBll();
            if (Session["name"] != null)
            {
                return View(bll.ShowOrders(Session["name"].ToString()));
            }
            else
            {
                return Content("<script>alert('请您登录！！');location.href='/Home/Deng'</script>");
            }
        }
        public ActionResult GetByNameAll()
        {
            MemberBll bll=new MemberBll();
            return Json(bll.GetByNameAll(Session["name"].ToString()), JsonRequestBehavior.AllowGet);
        }
    }
}
